home *** CD-ROM | disk | FTP | other *** search
/ Mac Format 1995 June / MacFormat 25.iso / Shareware City / Developers / OutOfPhase1.1 Source / OutOfPhase Folder / WaveTableStorageDisplay.c < prev    next >
Text File  |  1994-08-23  |  9KB  |  263 lines

  1. /* WaveTableStorageDisplay.c */
  2. /*****************************************************************************/
  3. /*                                                                           */
  4. /*    Out Of Phase:  Digital Music Synthesis on General Purpose Computers    */
  5. /*    Copyright (C) 1994  Thomas R. Lawrence                                 */
  6. /*                                                                           */
  7. /*    This program is free software; you can redistribute it and/or modify   */
  8. /*    it under the terms of the GNU General Public License as published by   */
  9. /*    the Free Software Foundation; either version 2 of the License, or      */
  10. /*    (at your option) any later version.                                    */
  11. /*                                                                           */
  12. /*    This program is distributed in the hope that it will be useful,        */
  13. /*    but WITHOUT ANY WARRANTY; without even the implied warranty of         */
  14. /*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          */
  15. /*    GNU General Public License for more details.                           */
  16. /*                                                                           */
  17. /*    You should have received a copy of the GNU General Public License      */
  18. /*    along with this program; if not, write to the Free Software            */
  19. /*    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.              */
  20. /*                                                                           */
  21. /*    Thomas R. Lawrence can be reached at tomlaw@world.std.com.             */
  22. /*                                                                           */
  23. /*****************************************************************************/
  24.  
  25. #include "MiscInfo.h"
  26. #include "Audit.h"
  27. #include "Debug.h"
  28. #include "Definitions.h"
  29.  
  30. #include "WaveTableStorageDisplay.h"
  31. #include "Array.h"
  32. #include "Memory.h"
  33. #include "DataMunging.h"
  34.  
  35.  
  36. struct WaveTableStorDispRec
  37.     {
  38.         NumBitsType            NumBits;
  39.         long                        FramesPerTable;
  40.         ArrayRec*                TableArray; /* of largefixedsigned[]'s */
  41.     };
  42.  
  43.  
  44. /* create a new, empty wave table storage display object */
  45. WaveTableStorDispRec*        NewWaveTableStorDisp(NumBitsType NumBits, long NumFrames)
  46.     {
  47.         WaveTableStorDispRec*    StorDisp;
  48.  
  49.         ERROR((NumBits != eSample8bit) && (NumBits != eSample16bit),PRERR(ForceAbort,
  50.             "NewWaveTableStorDisp:  bad number of bits"));
  51.         ERROR((NumFrames != 2) && (NumFrames != 4) && (NumFrames != 8) && (NumFrames != 16)
  52.             && (NumFrames != 32) && (NumFrames != 64) && (NumFrames != 128)
  53.             && (NumFrames != 256) && (NumFrames != 512) && (NumFrames != 1024)
  54.             && (NumFrames != 2048) && (NumFrames != 4096) && (NumFrames != 8192)
  55.             && (NumFrames != 16384) && (NumFrames != 32768) && (NumFrames != 65536),
  56.             PRERR(ForceAbort,"NewWaveTableStorDisp:  bad number of frames"));
  57.         StorDisp = (WaveTableStorDispRec*)AllocPtrCanFail(sizeof(WaveTableStorDispRec),
  58.             "WaveTableStorDispRec");
  59.         if (StorDisp == NIL)
  60.             {
  61.              FailurePoint1:
  62.                 return NIL;
  63.             }
  64.         StorDisp->NumBits = NumBits;
  65.         StorDisp->FramesPerTable = NumFrames;
  66.         StorDisp->TableArray = NewArray();
  67.         if (StorDisp->TableArray == NIL)
  68.             {
  69.              FailurePoint2:
  70.                 ReleasePtr((char*)StorDisp);
  71.                 goto FailurePoint1;
  72.             }
  73.         return StorDisp;
  74.     }
  75.  
  76.  
  77. /* dispose of the wave table storage object */
  78. void                                        DisposeWaveTableStorDisp(WaveTableStorDispRec* StorDisp)
  79.     {
  80.         long                                    Limit;
  81.         long                                    Scan;
  82.  
  83.         CheckPtrExistence(StorDisp);
  84.         Limit = ArrayGetLength(StorDisp->TableArray);
  85.         for (Scan = 0; Scan < Limit; Scan += 1)
  86.             {
  87.                 ReleasePtr((char*)ArrayGetElement(StorDisp->TableArray,Scan));
  88.             }
  89.         DisposeArray(StorDisp->TableArray);
  90.         ReleasePtr((char*)StorDisp);
  91.     }
  92.  
  93.  
  94. /* get the number of frames per table */
  95. long                                        WaveTableStorDispNumFramesPerTable(WaveTableStorDispRec* StorDisp)
  96.     {
  97.         CheckPtrExistence(StorDisp);
  98.         return StorDisp->FramesPerTable;
  99.     }
  100.  
  101.  
  102. /* get the number of tables */
  103. long                                        WaveTableStorDispNumTables(WaveTableStorDispRec* StorDisp)
  104.     {
  105.         CheckPtrExistence(StorDisp);
  106.         return ArrayGetLength(StorDisp->TableArray);
  107.     }
  108.  
  109.  
  110. /* get the number of bits for the wave table */
  111. NumBitsType                            WaveTableStorDispNumBits(WaveTableStorDispRec* StorDisp)
  112.     {
  113.         CheckPtrExistence(StorDisp);
  114.         return StorDisp->NumBits;
  115.     }
  116.  
  117.  
  118. /* change the number of bits for the wave table */
  119. void                                        SetWaveTableStorDispNumBits(WaveTableStorDispRec* StorDisp,
  120.                                                     NumBitsType NewNumBits)
  121.     {
  122.         CheckPtrExistence(StorDisp);
  123.         ERROR((NewNumBits != eSample8bit) && (NewNumBits != eSample16bit),PRERR(ForceAbort,
  124.             "SetWaveTableStorDispNumBits:  bad number of bits"));
  125.         StorDisp->NumBits = NewNumBits;
  126.     }
  127.  
  128.  
  129. /* get a reference to a table.  this is NOT copied, and there is no type information. */
  130. /* The last element is a repeat of the first, and is provided for making anti-aliasing */
  131. /* more efficient. */
  132. largefixedsigned*                WaveTableStorDispGetTable(WaveTableStorDispRec* StorDisp,
  133.                                                     long Index)
  134.     {
  135.         CheckPtrExistence(StorDisp);
  136.         ERROR((Index < 0) || (Index >= WaveTableStorDispNumTables(StorDisp)),
  137.             PRERR(ForceAbort,"WaveTableStorDispGetTable:  index out of range"));
  138.         return (largefixedsigned*)ArrayGetElement(StorDisp->TableArray,Index);
  139.     }
  140.  
  141.  
  142. /* append a new (zeroed out) table to the end of the array */
  143. MyBoolean                                WaveTableStorDispAppendEntry(WaveTableStorDispRec* StorDisp)
  144.     {
  145.         largefixedsigned*            NewSlice;
  146.         long                                    Scan;
  147.  
  148.         CheckPtrExistence(StorDisp);
  149.         NewSlice = (largefixedsigned*)AllocPtrCanFail(sizeof(largefixedsigned)
  150.             * (StorDisp->FramesPerTable + 1),"WaveTableStorDispRec slice");
  151.         /* + 1 used for the antialiasing slot */
  152.         if (NewSlice == NIL)
  153.             {
  154.              FailurePoint1:
  155.                 return False;
  156.             }
  157.         if (!ArrayAppendElement(StorDisp->TableArray,NewSlice))
  158.             {
  159.              FailurePoint2:
  160.                 ReleasePtr((char*)NewSlice);
  161.                 goto FailurePoint1;
  162.             }
  163.         for (Scan = 0; Scan < StorDisp->FramesPerTable + 1; Scan += 1)
  164.             {
  165.                 PRNGCHK(NewSlice,&(NewSlice[Scan]),sizeof(NewSlice[Scan]));
  166.                 NewSlice[Scan] = 0;
  167.             }
  168.         return True;
  169.     }
  170.  
  171.  
  172. /* put a value into a frame in a table */
  173. void                                        WaveTableStorDispSetFrame(WaveTableStorDispRec* StorDisp,
  174.                                                     long TableIndex, long FrameIndex, largefixedsigned Value)
  175.     {
  176.         largefixedsigned*            TheSlice;
  177.  
  178.         CheckPtrExistence(StorDisp);
  179.         ERROR((TableIndex < 0) || (TableIndex >= WaveTableStorDispNumTables(StorDisp)),
  180.             PRERR(ForceAbort,"WaveTableStorDispSetFrame:  table index out of range"));
  181.         ERROR((FrameIndex < 0) || (FrameIndex >= StorDisp->FramesPerTable),
  182.             PRERR(ForceAbort,"WaveTableStorDispSetFrame:  frame in table index out of range"));
  183.         TheSlice = WaveTableStorDispGetTable(StorDisp,TableIndex);
  184.         PRNGCHK(TheSlice,&(TheSlice[FrameIndex]),sizeof(TheSlice[FrameIndex]));
  185.         TheSlice[FrameIndex] = Value;
  186.         if (FrameIndex == 0)
  187.             {
  188.                 PRNGCHK(TheSlice,&(TheSlice[StorDisp->FramesPerTable]),
  189.                     sizeof(TheSlice[StorDisp->FramesPerTable]));
  190.                 TheSlice[StorDisp->FramesPerTable] = Value; /* anti-aliasing loopback value */
  191.             }
  192.     }
  193.  
  194.  
  195. /* get a value from a frame in a table */
  196. largefixedsigned                WaveTableStorDispGetFrame(WaveTableStorDispRec* StorDisp,
  197.                                                     long TableIndex, long FrameIndex)
  198.     {
  199.         largefixedsigned*            TheSlice;
  200.  
  201.         CheckPtrExistence(StorDisp);
  202.         ERROR((TableIndex < 0) || (TableIndex >= WaveTableStorDispNumTables(StorDisp)),
  203.             PRERR(ForceAbort,"WaveTableStorDispGetFrame:  table index out of range"));
  204.         ERROR((FrameIndex < 0) || (FrameIndex >= StorDisp->FramesPerTable),
  205.             PRERR(ForceAbort,"WaveTableStorDispGetFrame:  frame in table index out of range"));
  206.         TheSlice = WaveTableStorDispGetTable(StorDisp,TableIndex);
  207.         PRNGCHK(TheSlice,&(TheSlice[FrameIndex]),sizeof(TheSlice[FrameIndex]));
  208.         return TheSlice[FrameIndex];
  209.     }
  210.  
  211.  
  212. /* make a duplicate of the wave table */
  213. WaveTableStorDispRec*        WaveTableStorDispDuplicate(WaveTableStorDispRec* Original)
  214.     {
  215.         WaveTableStorDispRec*    Copy;
  216.         long                                    Limit;
  217.         long                                    Scan;
  218.  
  219.         CheckPtrExistence(Original);
  220.         Copy = (WaveTableStorDispRec*)AllocPtrCanFail(sizeof(WaveTableStorDispRec),
  221.             "WaveTableStorDispRec");
  222.         if (Copy == NIL)
  223.             {
  224.              FailurePoint1:
  225.                 return NIL;
  226.             }
  227.         Copy->NumBits = Original->NumBits;
  228.         Copy->FramesPerTable = Original->FramesPerTable;
  229.         Copy->TableArray = NewArray();
  230.         if (Copy->TableArray == NIL)
  231.             {
  232.              FailurePoint2:
  233.                 ReleasePtr((char*)Copy);
  234.                 goto FailurePoint1;
  235.             }
  236.         Limit = ArrayGetLength(Original->TableArray);
  237.         for (Scan = 0; Scan < Limit; Scan += 1)
  238.             {
  239.                 char*                                    Temp;
  240.  
  241.                 Temp = CopyPtr((char*)ArrayGetElement(Original->TableArray,Scan));
  242.                 if (Temp == NIL)
  243.                     {
  244.                      FailurePoint3:
  245.                         Limit = ArrayGetLength(Copy->TableArray);
  246.                         for (Scan = 0; Scan < Limit; Scan += 1)
  247.                             {
  248.                                 ReleasePtr((char*)ArrayGetElement(Copy->TableArray,Scan));
  249.                             }
  250.                         DisposeArray(Copy->TableArray);
  251.                         goto FailurePoint2;
  252.                     }
  253.                 SetTag(Temp,"WaveTableStorDispRec:  slice");
  254.                 if (!ArrayAppendElement(Copy->TableArray,Temp))
  255.                     {
  256.                      FailurePoint3a:
  257.                         ReleasePtr((char*)Temp);
  258.                         goto FailurePoint3;
  259.                     }
  260.             }
  261.         return Copy;
  262.     }
  263.